package cn.wxz1997.permission.controller;

import cn.wxz1997.permission.dto.PermissionMenuDTO;
import cn.wxz1997.permission.model.User;
import cn.wxz1997.permission.pojo.Permission;
import cn.wxz1997.permission.service.PermissionService;
import cn.wxz1997.permission.service.UserService;
import cn.wxz1997.permission.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: ${todo}
 * @Author: wxz1997
 * @Date 18-8-10下午3:47
 */
@Slf4j
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private UserService userService;

    @GetMapping
    public Result<List<Permission>> list(){
        return Result.success(permissionService.list());
    }

    @GetMapping("/tree")
    public Result<List<PermissionMenuDTO>> tree(){
        return Result.success(permissionService.getPermissionTree());
    }

    /**
     * 获取个人的权限菜单
     * @return
     */
    @GetMapping("/info")
    public Result<List<PermissionMenuDTO>> info(){
        User user = userService.getCurrentUser();
        return Result.success(permissionService.getMenuTree(user.getId()));
    }

    @PostMapping
    public Result<Permission> add(@RequestBody Permission permission){
        log.info("permission:" + permission);
        return Result.success(permissionService.add(permission));
    }

    @PutMapping("/{id}")
    public Result<Permission> update(@RequestBody Permission permission){
        log.info("permission:" + permission);
        return Result.success(permissionService.update(permission));
    }

    @GetMapping("/{id}")
    public Result<Permission> selectOne(@PathVariable Integer id){
        return Result.success(permissionService.selectById(id));
    }

    @DeleteMapping("/{id}")
    public Result delte(@PathVariable Integer id){
        return Result.success(permissionService.delete(id));
    }
}
